package com.git.datasource;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;

public class DynamicDataAspect {
	private String[] slaveMethodNames = {"query","select","get"};
	
	public void before(JoinPoint point){
		//如果已经设置了值，就略过
		if (StringUtils.isNotBlank(DynamicDataHolder.getKey())) {
			return;
		}
		//判读是否是上面的那些开头，如果是就是slave，其余都是master
		if (isSlave(point.getSignature().getName())) {
			DynamicDataHolder.markSlave();
		}else{
			DynamicDataHolder.markMaster();
		}
	}
	private boolean isSlave(String name) {
		for (String slaveMethodName : slaveMethodNames) {
			if (StringUtils.startsWith(name, slaveMethodName)) {
				return true;
			}
		}
		return false;
	}
}
